package com.example.rxjavademo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initStudent();
    }
    private void initStudent(){
        Observable.fromArray("jnadn","jdeih","huf","uigffu")
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Throwable {
                        Log.d(TAG,"姓名："+s);
                    }
                });
    }
    private void initView(){
        //创建观察者
        io.reactivex.rxjava3.core.Observer<String> observer=new Observer<String>() {
//监听到事件被订阅时回调，RxJava的新回调方法
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }
//监听到的事件发生步骤时回调
            @Override
            public void onNext(@NonNull String s) {
                Log.d(TAG,"内容："+s);

            }
//监听到事件处理异常时回调
            @Override
            public void onError(@NonNull Throwable e) {
                e.printStackTrace();

            }
//监听到事件完成时回调
            @Override
            public void onComplete() {
                Log.d(TAG,"运行完成");

            }
        };
        //创建被观察者
       /* Observable<String> observable=Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
                //创建事件序列
                emitter.onNext("Hello");
                emitter.onNext("Hi");
                emitter.onNext("alojf");
                emitter.onNext("hhhhh");
                //事件完成
                emitter.onComplete();
            }
        });*/
        //传多个参数
        Observable<String> observable=Observable.just("hello","hi");
        //传入数组
        Observable<String> observable1=Observable.fromArray("hello","jekg");


        //创建订阅
        //observable.subscribe(observer);
       // observable1.subscribe(observer);
        observable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Throwable {

            }
        });

    }
}